package com.renwei.yunlong.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.renwei.yunlong.R;
import com.renwei.yunlong.activity.contacts.CompanyContactActivity;
import com.renwei.yunlong.bean.EnterpriseBean;
import com.renwei.yunlong.utils.StringUtils;
import com.renwei.yunlong.view.CircleTextImage;

import java.util.ArrayList;
import java.util.List;


/**
 * Project :  yunaandroid.
 * Package name: com.renwei.yunlong.adapter
 * Created by :  Benjamin.
 * Created time: 2017/10/31 13:20
 * Changed by :  Benjamin.
 * Changed time: 2017/10/31 13:20
 * Class description:
 */

public class FriendCompanyAdapter extends RecyclerView.Adapter<FriendCompanyAdapter.ViewHolder> {
    private int from_type = 0;
    private Context context;
    private List<EnterpriseBean.RowsBean> rowsBeanList = new ArrayList<>();
    private String thisCompanyType;


    public FriendCompanyAdapter(Context context, int from_type, String thisCompanyType) {
        this.from_type = from_type;
        this.context = context;
        this.thisCompanyType = thisCompanyType;
    }


    public void clean() {
        this.rowsBeanList.clear();
        notifyDataSetChanged();
    }

    public void refresh(List<EnterpriseBean.RowsBean> rows) {
        this.rowsBeanList = rows;
        notifyDataSetChanged();
    }

    public void addAll(List<EnterpriseBean.RowsBean> rows) {
        if (rows != null) {
            this.rowsBeanList.addAll(rows);
            notifyDataSetChanged();
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_select_company, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final EnterpriseBean.RowsBean rows = rowsBeanList.get(position);
        holder.mIvHead.setText4CircleImage(String.valueOf(rows.getServerName()));
        holder.mTvCompanyName.setText(StringUtils.value(rows.getServerName()));
        holder.mTvCount.setText("");
        if ("2".equals(thisCompanyType)) {
            if ("1".equals(rows.getBindFlag())) {
                holder.mIvType.setImageResource(R.mipmap.icon_company_hyfws);
                holder.mIvHead.setTextCircleColor(Color.parseColor("#a9db34"));
            } else if ("2".equals(rows.getBindFlag())) {
                holder.mIvType.setImageResource(R.mipmap.icon_company_zjfws);
                holder.mIvHead.setTextCircleColor(Color.parseColor("#ff7d8c"));
            }
        } else {
            if ("1".equals(rows.getBindFlag())) {
                holder.mIvType.setImageResource(R.mipmap.icon_company_hyyz);
                holder.mIvHead.setTextCircleColor(Color.parseColor("#a9db34"));
            } else if ("2".equals(rows.getBindFlag())) {
                holder.mIvType.setImageResource(R.mipmap.icon_company_zjyz);
                holder.mIvHead.setTextCircleColor(Color.parseColor("#ff7d8c"));
            }
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CompanyContactActivity.OpenActivity(context, StringUtils.value(rows.getCompanyCode()), StringUtils.value(thisCompanyType), StringUtils.value(rows.getServerName()), from_type);
            }
        });

    }

    @Override
    public int getItemCount() {
        if (rowsBeanList == null) {
            return 0;
        } else {
            return rowsBeanList.size();
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        CircleTextImage mIvHead;
        TextView mTvCount;
        TextView mTvCompanyName;
        ImageView mIvType;

        public ViewHolder(View itemView) {
            super(itemView);
            mIvHead = itemView.findViewById(R.id.iv_head);
            mTvCount = itemView.findViewById(R.id.tv_count);
            mTvCompanyName = itemView.findViewById(R.id.tv_company_name);
            mIvType = itemView.findViewById(R.id.iv_type);
        }
    }
}
